//存储基本信息
export default class Storage {
    static instance;
    static propertyList = ['property', 'config', 'loginData'];
    static storeList = [
        { name: "applyList", key: "-friend", tip: "好友申列表" },
        { name: "talkList", key: "", tip: "聊天记录" },
        { name: "friendList", key: "-friendList", tip: "好友列表" }
    ];
    constructor() {
        if (!Storage.instance) {
            Storage.instance = this;
            this.property = {};
            this.config = {};
            this.loginData = {};
            this.personKey = ''
        }
        return Storage.instance;
    }
    /*设置Storage实例属性*/
    setStorageProperty(key, value) {
        this[key] = value;
        //设置用户个人key
        if (key == 'property') { this.personKey = `${value.member_id}~${value.sertype}~${value.port}` }
        return { code: 200, data:"成功！" }
    }
    /*会话类型存储（关闭浏览器清空）*/
    saveBySessionStorage(name, value) {
        sessionStorage.setItem(name, JSON.stringify(value));
        return { code: 200, data: "成功！" }
    }
    /*永久存储*/
    saveByLocalStorage(name, value) {
        if(!this.personKey){console.log("personKey为空！");return {code:301,data:'personKey为空！'}}
        const item = Storage.storeList.find(item => { if (item.name == name) { return item } })
        if (!item) { return { code: 301, data: name + "不存在！" } }
        const key = this.personKey + item.key;
        localStorage.setItem(key, JSON.stringify(value));
        return { code: 200, data: "成功！" }
    }
    /*从存储中取出数据*/
    getSessionStorage(name) {
        let data = sessionStorage.getItem(name);
        if (!data) { return { code: 301, data: name + "不存在！" } }
        data = JSON.parse(data);
        return { code: 200, data }
    }
    /*从存储中取出数据*/
    getLocalStorage(name) {
        if(!this.personKey){console.log("personKey为空！");return {code:301,data:'personKey为空！'}}
        const item = Storage.storeList.find(item => { if (item.name == name) { return item } })
        if (!item) { return { code: 301, data: name + "不存在！" } }
        const key = this.personKey + item.key;
        let data = localStorage.getItem(key);
        if (!data) { return { code: 301, data: "该数据不存在！" } }
        data = JSON.parse(data);
        return { code: 200, data }
    }
    /*从本地存储载入数据*/
    init(that) {
        that.talkObject.initTalkList();
        that.friendObject.initApplyList();
        return { code: 200, data:"成功！" }
    }
    /*设置对象的基本属性*/
    setProperty(that) {
        that.talkObject.setProperty(this.property);
        that.friendObject.setProperty(this.property);
        console.log(this.property.member_id, this.loginData.token, this.loginData.key,
            this.config.h_url, this.config.a_url, this.config.e_url)
        that.controller.setProperty(
            this.property.member_id, this.loginData.token, this.loginData.key,
            this.config.h_url, this.config.a_url, this.config.e_url);
            return { code: 200, data:"成功！" }
    }
    clear(that){
        that.talkObject.clear();
        that.friendObject.clear();
        that.controller.clear();
        return { code: 200, data:"成功！" }
    }

}